<?php
include("../includes/DBFunctions.php");
include("../includes/GXMLParser.php");
include("../includes/getCredentials.php");

function displayPopulationFunctor( $selectedSpecies ) {
	$gc = new GetCredentials;
	$uid = $gc->getUserID();
	$db = new DBFunctions;
	$db->connectDB();
	$result = $db->query("call displayPopulation(".$selectedSpecies.",".$uid.",@a)" );
	if (!$result || mysqli_num_rows($result) == 0) {
		$error = "";
		$errors = $db->query("SELECT @a");
		if ($errors != 0) {
			while( $line = mysqli_fetch_array( $errors, MYSQL_ASSOC ) ) {
				$error = $line['@a'];
			}
			echo "alert('Failed stored procedure call (displayPopulationFunctor) ".$errors."');";
		}
	}
	$xml = new GXMLParser;
	$xml->openXML();
	while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
		$xml->addFlag( $line['Population'], $line['Latitude'], $line['Longitude'], $line['CommonName'], $line['Name']  );
	}
	$xml->closeXML("displayPopulations",$selectedSpecies);
	$db->closeDB();
}

if( !isset($_POST['selectSpecies']) || $_POST['selectSpecies'] == '') {
	die("alert('No species was selected. Please try again.');");
}
$sid = $_POST['selectSpecies'];

displayPopulationFunctor( $sid );
?>